<?php
Globals::requireController('Admin');
Globals::requireTable('Product');
Globals::requireTable('Days');
Globals::requireTable('Account');
Globals::requireTable('Order');

class OrderController extends AdminController
{
    protected $productTable;
    protected $daysTable;
    protected $accountTable;
    protected $orderTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->productTable = new ProductTable($config);
		$this->daysTable = new DaysTable($config);
		$this->accountTable = new AccountTable($config);
		$this->orderTable = new OrderTable($config);
	}
	
	public function indexAction()
	{
	    $where['status'] = 1;
	    $count = $this->orderTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $data = $this->orderTable->listPage($where, 'id DESC', $pageSize, $pageId);
	    foreach ($data as $k=>$v){
	        $tmp_days = $this->daysTable->getRow($v['did']);
	        $data[$k]['days'] = $tmp_days;
	        
	        $tmp_account = $this->accountTable->getRow($v['aid']);
	        $data[$k]['account'] = $tmp_account;
	    }
	    $this->view->data = $data;
	}
	
	public function detailAction()
	{
	    $id = $this->getIntParam('id');
	    $order = $this->orderTable->getRow($id);
	    if($order['id'] > 0){
	        $order['user'] = $this->userTable->getRow($order['uid']);
	        $order['days'] = $this->daysTable->getRow($order['did']);
	        $order['product'] = $this->productTable->getRow($order['pid']);
	        $order['account'] = $this->accountTable->getRow($order['aid']);
	        $this->view->order = $order;
	    }else{
	        $this->redirectToAction();
	        exit();
	    }
	}
	
	
}
